//
//  WRWaterLayout.m
//  01-瀑布流
//
//  Created by 紫荆秋雪 on 16/3/6.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

#import "WRWaterLayout.h"

/** 默认的列数 */
static const NSInteger WRDefaultColumnCount = 3;
/** 每一列之间的间距 */
static const CGFloat WRDefaultColumnMargin = 10;
/** 每一行之间的间距 */
static const CGFloat WRDefaultRowMargin = 10;
/** 边缘间距 */
static const UIEdgeInsets WRDefaultEdgeInsets = {10, 10, 10, 10};

@interface WRWaterLayout ()
/** 存储所有列的高度 */
@property (nonatomic,strong) NSMutableArray *columnHeightArray;

/** 存储所有Item的属性 */
@property (nonatomic,strong) NSMutableArray *attrsArray;

@end

@implementation WRWaterLayout

#pragma mark - 懒加载
- (NSMutableArray *)columnHeightArray
{

    if (_columnHeightArray == nil) {
        
        _columnHeightArray = [NSMutableArray array];
    }
    return _columnHeightArray;
}

- (NSMutableArray *)attrsArray
{

    if (_attrsArray == nil) {
        
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

#pragma mark - 初始化
- (void)prepareLayout
{

    [super prepareLayout];
    
    //清除以前计算的所有高度
    [self.columnHeightArray removeAllObjects];
    
    //重新设置每一列的“初始高度”
    for (NSInteger i = 0; i < WRDefaultColumnCount; i++) {
        [self.columnHeightArray addObject:@(WRDefaultEdgeInsets.top)];
    }
    
    //清除之前所有的“布局属性”
    [self.attrsArray removeAllObjects];
    
    //开始创建每一个cell对应的布局属性
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    
    for (NSInteger i = 0 ; i < count; i++) {
        //创建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        //设置对应indexPath位置cell对应的布局属性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        
        //添加到“属性数组”中
        [self.attrsArray addObject:attrs];
    }
    
}

#pragma mark - 设置每一个indexPath位置上的Item属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //1、获取每一个indexPath位置上的Item
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    //2、设置frame
    /** collection的宽度 */
    CGFloat collectionW = self.collectionView.frame.size.width;
    
    /** Item的宽度 */
    CGFloat w = ((collectionW - WRDefaultEdgeInsets.left - WRDefaultEdgeInsets.right) - (WRDefaultColumnCount - 1) * WRDefaultColumnMargin) / WRDefaultColumnCount;

    /** item的高度 */
    CGFloat h = 50 + arc4random_uniform(100);
    
    //找出高度最短的那一列
    NSInteger destColumn = 0;
    CGFloat columnHeightMin = [self.columnHeightArray[0] doubleValue];
    
    for (NSInteger i = 1; i < WRDefaultColumnCount; i++) {
        CGFloat columnHeight = [self.columnHeightArray[i] doubleValue];
        
        if (columnHeightMin > columnHeight) {
            columnHeightMin = columnHeight;
            //获得“高度最短”的列数
            destColumn = i;
        }
    }
    
    CGFloat x = WRDefaultEdgeInsets.left + (WRDefaultColumnMargin + w) * destColumn;
    CGFloat y = columnHeightMin;
    
    if (y != WRDefaultEdgeInsets.top) {
        y += WRDefaultColumnMargin;
    }
    
    //设置属性的frame
    attrs.frame = CGRectMake(x, y, w, h);
    
    //更新最短那列的高度
    self.columnHeightArray[destColumn] = @(CGRectGetMaxY(attrs.frame));
    
    return attrs;
}

/**
 *  决定cell的排布
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{

    return self.attrsArray;
}

- (CGSize)collectionViewContentSize
{

    //假设0列最高
    CGFloat maxHeight = [self.columnHeightArray[0] doubleValue];
    
    //通过循环遍历来寻找最高的列
    for (NSInteger i = 1; i <WRDefaultColumnCount; i++) {
        
        CGFloat height = [self.columnHeightArray[i] doubleValue];
        if (height > maxHeight) {
            
            maxHeight = height;
        }

    }
    return CGSizeMake(0, maxHeight + WRDefaultEdgeInsets.bottom);
}

@end
